package com.example.c_input;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo1FileInputStream {
    public static void main(String[] args) {
        method1();
        System.out.println("method2");
        method2();
    }

    private static void method1() {
        try {
            FileInputStream fis = new FileInputStream("ab.txt");
//            int data = fis.read();
//            System.out.println(data);
//
//            int data2 = fis.read();
//            System.out.println(data2);

            int len;
            while ((len=fis.read())!=-1) {
                System.out.println("read:" + len);
            }
            
            System.out.println(fis.read());

            fis.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    private static void method2() {
        try {
            FileInputStream fis = new FileInputStream("a.txt");
            //        这里读取时的数组长度一般为1024或者是1024的倍数
//            byte[] bytes = new byte[2];
//            int len1 = fis.read(bytes);
//            System.out.println(len1);
//            System.out.println(new String(bytes, 0, len1));
//
//            int len2 = fis.read(bytes);
//            System.out.println(len2);
//            System.out.println(new String(bytes, 0, len2));
//
//            int len3 = fis.read(bytes);
//            System.out.println(len3);
//            System.out.println(new String(bytes, 0, len3));
//            fis.close();

            byte[] bytes = new byte[1024];
            int len;
            while((len = fis.read(bytes))!= -1){
                System.out.println(len);
//                读取多少个就转换多少个
                System.out.println(new String(bytes, 0, len));
            }
            fis.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
